package cn.wolfcode.plus.util;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.ObjectUtils;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回给客户端具体的异常对象
 */
@Setter
@Getter
@ToString
public class ErrorResponse {

    private int code;
    private int status;
    private String message;
    private String path;
    private Instant timestamp;
    private Map<String,Object> data = new HashMap<>();

    public ErrorResponse() {
    }

    public ErrorResponse(BaseException ex, String path){
        this(ex.getError().getCode(),ex.getError().getStatus().value(),ex.getError().getMessage(),path,ex.getData());
    }

    public ErrorResponse(int code, int status, String message, String path, HashMap<String, Object> data) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.path = path;
        this.timestamp = Instant.now();
        if (!ObjectUtils.isEmpty(data)) {
            this.data.putAll(data);
        }
    }
}
